//
//  VPRoundViewController.m
//  VPPublicUntilitisForPod
//
//  Created by vernepung on 2017/8/28.
//  Copyright © 2017年 vernepung. All rights reserved.
//

#import "VPRoundViewController.h"
#import "UtilsMacro.h"

@interface VPRoundViewController ()<CAAnimationDelegate>

@property (strong, nonatomic) UIButton *showButton;
@property (strong, nonatomic) CAShapeLayer *roundLayer;
@end

@implementation VPRoundViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _showButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _showButton.frame = CGRectMake(300, 300, 80, 80);
    _showButton.layer.cornerRadius = 40;
    _showButton.layer.masksToBounds = YES;
    [_showButton setTitle:@"点击" forState:0];
    _showButton.backgroundColor = [UIColor redColor];
    UIPanGestureRecognizer *penGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(showButtonPen:)];
    [_showButton addGestureRecognizer:penGesture];
    //    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(showButtonSwipe:)];
    //    [_showButton addGestureRecognizer:swipeGesture];
    [_showButton addTarget:self action:@selector(btnShowClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_showButton];
    
    // Do any additional setup after loading the view.
}

- (void)btnShowClicked:(UIButton *)sender {
    UIBezierPath *startPath = [UIBezierPath bezierPathWithOvalInRect:_showButton.frame];
    if (!self.showButton.tag) {
        CGFloat x = MAX(self.showButton.frame.origin.x, self.view.frame.size.width - self.showButton.frame.origin.x);
        CGFloat y = MAX(self.showButton.frame.origin.y, self.view.frame.size.height - self.showButton.frame.origin.y);
        CGFloat radius = sqrtf(pow(x, 2) + pow(y, 2));
        UIBezierPath *endPath = [UIBezierPath bezierPathWithArcCenter:self.view.center radius:radius startAngle:0 endAngle:2*M_PI clockwise:YES];
        self.roundLayer = [CAShapeLayer layer];
        self.roundLayer.path = endPath.CGPath;
            [self.view.layer insertSublayer:self.roundLayer below:self.showButton.layer];
//        self.view.layer.mask = self.roundLayer;
        CABasicAnimation *animation = [[CABasicAnimation alloc]init];
        animation.delegate = self;
        animation.duration = 0.8f;
        animation.timingFunction = [CAMediaTimingFunction  functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        animation.fromValue = (__bridge id)startPath.CGPath;
        animation.toValue = (__bridge id)endPath.CGPath;
        [self.roundLayer addAnimation:animation forKey:@"path"];
        self.showButton.tag = 1;
    }else{
        
        CGFloat radius = sqrtf(pow(kMainBoundsWidth,2) + pow(kMainBoundsHeight,2)) / 2;
//
        UIBezierPath *endPath = [UIBezierPath bezierPathWithArcCenter:self.navigationController.view.center radius:radius startAngle:0 endAngle:2*M_PI clockwise:YES];
        self.roundLayer.path = startPath.CGPath;
        //    [self.view.layer insertSublayer:roundLayer above:self.showButton.layer];
//        self.view.layer.mask = self.roundLayer;
        CABasicAnimation *animation = [[CABasicAnimation alloc]init];
        animation.delegate = self;
        animation.duration = 0.8f;
        animation.timingFunction = [CAMediaTimingFunction  functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        animation.fromValue = (__bridge id)endPath.CGPath;
        animation.toValue = (__bridge id)startPath.CGPath;
        [animation setValue:self.showButton forKey:@"vpAnimationEnd"];
        [self.roundLayer addAnimation:animation forKey:@"path"];
        self.showButton.tag = 0;
    }
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    UIButton *button = (UIButton *)[anim valueForKey:@"vpAnimationEnd"];
    if (button) {
        [self.roundLayer removeFromSuperlayer];
    }
}

//- (void)showButtonSwipe:(UISwipeGestureRecognizer *)recoginizer {
//
//}

- (void)showButtonPen:(UIPanGestureRecognizer *)recognizer {
    
    CGPoint originPoint = recognizer.view.center;
    CGPoint translation = [recognizer translationInView:recognizer.view];
    CGPoint newPoint = CGPointMake(originPoint.x + translation.x, originPoint.y + translation.y);
    newPoint.x = MAX(recognizer.view.frame.size.width / 2, newPoint.x);
    newPoint.x = MIN(self.view.frame.size.width - recognizer.view.frame.size.width / 2, newPoint.x);
    newPoint.y = MAX(recognizer.view.frame.size.height / 2, newPoint.y);
    newPoint.y = MIN(self.view.frame.size.height - recognizer.view.frame.size.height / 2, newPoint.y);
    
    recognizer.view.center = newPoint;
    [recognizer setTranslation:CGPointZero inView:self.view];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
